/* crypto/sha/sha1test.c */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
 * All rights reserved.
 *
 * This package is an SSL implementation written
 * by Eric Young (eay@cryptsoft.com).
 * The implementation was written so as to conform with Netscapes SSL.
 *
 * This library is free for commercial and non-commercial use as long as
 * the following conditions are aheared to.  The following conditions
 * apply to all code found in this distribution, be it the RC4, RSA,
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
 * included with this distribution is covered by the same copyright terms
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
 *
 * Copyright remains Eric Young's, and as such any Copyright notices in
 * the code are not to be removed.
 * If this package is used in a product, Eric Young should be given attribution
 * as the author of the parts of the library used.
 * This can be in the form of a textual message at program startup or
 * in documentation (online or textual) provided with the package.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes cryptographic software written by
 *     Eric Young (eay@cryptsoft.com)"
 *    The word 'cryptographic' can be left out if the rouines from the library
 *    being used are not cryptographic related :-).
 * 4. If you include any Windows specific code (or a derivative thereof) from
 *    the apps directory (application code) you must include an acknowledgement:
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
 *
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The licence and distribution terms for any publically available version or
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
 * copied and put under another distribution licence
 * [including the GNU Public Licence.]
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "e_os.h"

#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/ossl_typ.h>
#include <openssl/evp.h>
#include <stdio.h>

#include "sec_common.h"
#include "hard.h"

static char *test[] = {
    "abc",
    "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
    NULL,
};

static char *sha0_ret[] = {
    "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
    "d2516ee1acfa5baf33dfc1c471e438449ef134c8",
};


static char *sha0_bigret="3232affa48628a26653b5aaa44541fd90d690603";

static char *sha1_ret[] = {
    "a9993e364706816aba3e25717850c26c9cd0d89d",
    "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
};

static char sha1_stdata[] = {
		// DATA 2
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76};

static char sha1_stdata_128[] = {
		// DATA 2
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76,
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76,
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76,
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76,			//128
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76,
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76,
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76,
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76,			//128
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76,
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76,
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76,
		0x2C,0xB6,0xA0,0x6E,0x92,0xDC,0x95,0x70,0x24,0x2E,0x4E,0x77,0xA1,0x8D,0x6D,0xF0,
		0xBF,0xA1,0x63,0xB7,0x33,0xA6,0x81,0xDF,0xA8,0x6F,0x48,0x65,0x0A,0x25,0x1F,0x76,			//128
		};

static char stkey[] = {0x11,0x22,0x33,0x44,0x55,0x66};

static char *sha1_bigret="34aa973cd4c4daa4f61eeb2bdbad27316534016f";

static char sha1_stout[] = {
		// STD OUT 2
		0xf8,0x7b,0x6b,0x9a,0x7f,0x44,0x2d,0x26,0x88,0x6f,0xd1,0x5c,0xc3,0xf9,0xd0,0x2a,0xa3,0x4f,0xd8,0x88};

static char *pt(unsigned char *md);

/////////////////////////////////////////////////////////////////////////////////////
hmac_md5_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 16;

	capi.alg = H_MD5;
	capi.type  = T_HMAC;
	capi.as = INITFINAL;
	capi.key_len = 16;
	capi.iv_len = 16;
	capi.data_len = (( 0x10|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hmac(md5) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hmac(md5) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hmac_md5_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}

/////////////////////////////////////////////////////////////////////////////////////
hmac_sha0_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 20;

	capi.alg = H_SHA0;
	capi.type  = T_HMAC;
	capi.as = INITFINAL;
	capi.key_len = 20;
	capi.iv_len = 32;
	capi.data_len = (( 0x20|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hmac(sha0) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hmac(sha0) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hmac_sha0_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}

hmac_sha1_ssl_hard_speed(int fd)
{
	int tcnt=100,MNUM=1000;
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,*dt,iv_tmp[16];
	unsigned int i;
	struct timeval tv1,tv2;

	int cpid;
	int semval;

	int time,speed;

	capi.alg = H_SHA1;
	capi.type  = T_HMAC;
	capi.as = INITFINAL;
	capi.key_len = 20;
	capi.iv_len = 32;
	capi.data_len = (max_crypto_len-100)&0xfffffff0;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	for(i=0;i<tcnt-1;i++)
	{
		cpid = fork();
		if(cpid==0)
			break;
	}

	mem= malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

	if(cpid==0)
	{
		do{
			sem_getvalue(usem->sem,&semval);
		}
		while(semval==1);
	}else{
		sem_wait(usem->sem);
		gettimeofday(&tv1, NULL);
	}

	for(i=0;i<MNUM;i++)
	{
		memset((void *)(mem+CHEAD_SIZE+capi.key_len),0,capi.iv_len);

		ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);

		while(!(mem[3]&DDONE_MASK));
	}
	sem_wait(dsem->sem);

	if(cpid==0)
	{
		free((void *)mem);
		exit(0);
	}else{
		do{
			sem_getvalue(dsem->sem,&semval);
		}
		while(semval!=(MAX_SEM_CNT-tcnt));
		gettimeofday(&tv2, NULL);
		free((void *)mem);
	}

	sem_post(usem->sem);
	for(i=0;i<tcnt;i++)
	{
		sem_post(dsem->sem);
	}

	time = (tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec;

	speed = MNUM*tcnt*capi.data_len/time;
	speed*=8;

	spd_dbg("%s,sm1 encrypt data_len %d,tcnt %d,speed is %dMbps,time %d us\n",__FILE__,capi.data_len,tcnt,speed,time);

}

/////////////////////////////////////////////////////////////////////////////////////
hmac_sha1_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 20;

	capi.alg = H_SHA1;
	capi.type  = T_HMAC;
	capi.as = INITFINAL;
	capi.key_len = 20;
	capi.iv_len = 32;
	capi.data_len = (( 0x20|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hmac(sha1) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hmac(sha1) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hmac_sha1_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}
hash_sha1_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 20;

	capi.alg = H_SHA1;
	capi.type  = T_HASH;
	capi.as = INITFINAL;
	capi.key_len = 20;
	capi.iv_len = 32;
	capi.data_len = (( 0x20|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	//set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	//set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hash(sha1) enc:\n");
	for(i=68;i<20+68;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hash(sha1) enc:\n");
	for(i=68;i<20+68;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hash_sha1_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}

/////////////////////////////////////////////////////////////////////////////////////
hmac_sha224_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 28;

	capi.alg = H_SHA224;
	capi.type  = T_HMAC;
	capi.as = INITFINAL;
	capi.key_len = 28;
	capi.iv_len = 32;
	capi.data_len = (( 0x20|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hmac(sha224) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hmac(sha224) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hmac_sha224_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}

hmac_sha256_ssl_hard_speed(int fd)
{
	int tcnt=100,MNUM=1000;
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,*dt,iv_tmp[16];
	unsigned int i;
	struct timeval tv1,tv2;

	int cpid;
	int semval;

	int time,speed;

	capi.alg = H_SHA256;
	capi.type  = T_HMAC;
	capi.as = INITFINAL;
	capi.key_len = 32;
	capi.iv_len = 32;
	capi.data_len = (max_crypto_len-100)&0xfffffff0;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	for(i=0;i<tcnt-1;i++)
	{
		cpid = fork();
		if(cpid==0)
			break;
	}

	mem= malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);


/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

	if(cpid==0)
	{
		do{
			sem_getvalue(usem->sem,&semval);
		}
		while(semval==1);
	}else{
		sem_wait(usem->sem);
		gettimeofday(&tv1, NULL);
	}
	for(i=0;i<MNUM;i++)
	{
		memset((void *)(mem+CHEAD_SIZE+capi.key_len),0,capi.iv_len);

		ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);

		while(!(mem[3]&DDONE_MASK));

	}
	sem_wait(dsem->sem);

	if(cpid==0)
	{
		free((void *)mem);
		exit(0);
	}else{
		do{
			sem_getvalue(dsem->sem,&semval);
		}
		while(semval!=(MAX_SEM_CNT-tcnt));
		gettimeofday(&tv2, NULL);
		free((void *)mem);
	}

	sem_post(usem->sem);
	for(i=0;i<tcnt;i++)
	{
		sem_post(dsem->sem);
	}

	time = (tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec;

	speed = MNUM*tcnt*capi.data_len/time;
	speed*=8;

	spd_dbg("%s,sha256 enc data_len %d,tcnt %d,speed is %dMbps,time %d us\n",__FILE__,capi.data_len,tcnt,speed,time);

}
/////////////////////////////////////////////////////////////////////////////////////
hmac_sha256_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 32;

	capi.alg = H_SHA256;
	capi.type  = T_HMAC;
	capi.as = INITFINAL;
	capi.key_len = 32;
	capi.iv_len = 32;
	capi.data_len = (( 0x20|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hmac(sha256) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hmac(sha256) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hmac_sha256_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}

hash_sha256_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 32;

	capi.alg = H_SHA256;
	capi.type  = T_HASH;
	capi.as = INITFINAL;
	capi.key_len = 32;
	capi.iv_len = 32;
	capi.data_len = (( 0x20|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hash(sha256) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hash(sha256) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hash_sha256_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}
/////////////////////////////////////////////////////////////////////////////////////
hmac_sha384_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 48;

	capi.alg = H_SHA384;
	capi.type  = T_HMAC;
	capi.as = INITFINAL;
	capi.key_len = 48;
	capi.iv_len = 64;
	capi.data_len = (( 0x40|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hmac(sha384) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hmac(sha384) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hmac_sha384_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}
hash_sha384_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 48;

	capi.alg = H_SHA384;
	capi.type  = T_HASH;
	capi.as = INITFINAL;
	capi.key_len = 48;
	capi.iv_len = 64;
	capi.data_len = (( 0x40|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hash(sha384) enc:\n");
	for(i=64;i<20+64;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hash(sha384) enc:\n");
	for(i=64;i<20+64;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hash_sha384_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}

/////////////////////////////////////////////////////////////////////////////////////
hmac_sha512_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 64;

	capi.alg = H_SHA512;
	capi.type  = T_HMAC;
	capi.as = INITFINAL;
	capi.key_len = 64;
	capi.iv_len = 64;
	capi.data_len = (( 0x40|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hmac(sha512) enc:\n");
	for(i=0;i<144;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hmac(sha512) enc:\n");
	for(i=0;i<144;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hmac_sha512_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}

hash_sha512_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 64;

	capi.alg = H_SHA512;
	capi.type  = T_HASH;
	capi.as = INITFINAL;
	capi.key_len = 64;
	capi.iv_len = 64;
	capi.data_len = (( 0x40|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	//set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hash(sha512) enc:\n");
	for(i=0;i<144;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW 
	debug("\nafter hash(sha512) enc:\n");
	for(i=0;i<144;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hash_sha512_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}
/////////////////////////////////////////////////////////////////////////////////////
hmac_sm3_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;	// 存放加密输入的数据
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 32;

	capi.alg = H_SM3;
	capi.type  = T_HMAC;
	capi.as = INITFINAL;
	capi.key_len = 32;
	capi.iv_len = 32;
	// 设置数据长度为随机值，范围为 max_crypto_len 的一半，并对齐到 16 字节边界
	//capi.data_len = (( 0x20|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;


	//debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	unsigned char custom_key[32] = {
		0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
		0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
		0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
		0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00
	};
	unsigned char custom_iv[32] = {
		0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
		0x21, 0x43, 0x65, 0x87, 0xA9, 0xCB, 0xED, 0x0F,
		0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
		0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0, 0x00
	};
	unsigned char custom_data[8] = {
		0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE, 0xBA, 0xBE	};
	memcpy((void *)(mem+CHEAD_SIZE),custom_key,32);
	memcpy((void *)(mem+CHEAD_SIZE+capi.key_len),custom_iv,32);
	memcpy((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),custom_data,8);
	capi.data_len = 8;

	//set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	//set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	//set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hmac(sm3) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hmac(sm3) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hmac_sm3_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}

hash_sm3_ssl_hard_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out;
	unsigned int i;
	int cmpsize = 32;

	capi.alg = H_SM3;
	capi.type  = T_HASH;
	capi.as = INITFINAL;
	capi.key_len = 32;
	capi.iv_len = 32;
	capi.data_len = (( 0x20|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	//set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hash(sm3) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hash(sm3) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif
}

/////////////////////////////////////////////////////////////////////////////////////
hmac_sm3_ssl_hard_externkey_test(int fd)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,keytmp[32];
	unsigned int i;
	int cmpsize = 32;
	unsigned *space32;

	capi.alg = H_SM3;
	capi.type  = T_HMAC;
	capi.as = INITFINAL;
	capi.key_len = 0x80000000|32;
	capi.iv_len = 32;
	capi.data_len = (( 0x20|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(cmpsize);

	space32 = (unsigned *)(mem+CHEAD_SIZE);
	*space32 = 0xe0300000;					//need 16 byte 0~0xf first in cards
	space32++;
	*space32 = 0x80000000;

	for(i=0;i<32;i++)
	{
		keytmp[i]=i;
	}

	set_rand_mem((void *)(mem+CHEAD_SIZE+8),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+8+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before hmac(sm3) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+8+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter hmac(sm3) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	debug("hmac_sm3_ssl_hard  enc test ok!\n");

	free((void *)out);
	free((void *)mem);
}


hash_sha1_ssl_hard_speed(int fd)
{
	int tcnt=100,MNUM=1000;
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,*dt,iv_tmp[16];
	unsigned int i;
	struct timeval tv1,tv2;

	int cpid;
	int semval;

	int time,speed;

	capi.alg = H_SHA1;
	capi.type  = T_HASH;
	capi.as = INITFINAL;
	capi.key_len = 20;
	capi.iv_len = 32;
	capi.data_len = (max_crypto_len-100)&0xfffffff0;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	for(i=0;i<tcnt-1;i++)
	{
		cpid = fork();
		if(cpid==0)
			break;
	}

	mem= malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

	if(cpid==0)
	{
		do{
			sem_getvalue(usem->sem,&semval);
		}
		while(semval==1);
	}else{
		sem_wait(usem->sem);
		gettimeofday(&tv1, NULL);
	}

	for(i=0;i<MNUM;i++)
	{
		memset((void *)(mem+CHEAD_SIZE+capi.key_len),0,capi.iv_len);

		ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);

		while(!(mem[3]&DDONE_MASK));
	}
	sem_wait(dsem->sem);

	if(cpid==0)
	{
		free((void *)mem);
		exit(0);
	}else{
		do{
			sem_getvalue(dsem->sem,&semval);
		}
		while(semval!=(MAX_SEM_CNT-tcnt));
		gettimeofday(&tv2, NULL);
		free((void *)mem);
	}

	sem_post(usem->sem);
	for(i=0;i<tcnt;i++)
	{
		sem_post(dsem->sem);
	}

	time = (tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec;

	speed = MNUM*tcnt*capi.data_len/time;
	speed*=8;

	spd_dbg("%s,sm1 encrypt data_len %d,tcnt %d,speed is %dMbps,time %d us\n",__FILE__,capi.data_len,tcnt,speed,time);

}


hash_sha256_ssl_hard_speed(int fd)
{
	int tcnt=100,MNUM=1000;
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,*dt,iv_tmp[16];
	unsigned int i;
	struct timeval tv1,tv2;

	int cpid;
	int semval;

	int time,speed;

	capi.alg = H_SHA256;
	capi.type  = T_HASH;
	capi.as = INITFINAL;
	capi.key_len = 32;
	capi.iv_len = 32;
	capi.data_len = (max_crypto_len-100)&0xfffffff0;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	for(i=0;i<tcnt-1;i++)
	{
		cpid = fork();
		if(cpid==0)
			break;
	}

	mem= malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);


/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

	if(cpid==0)
	{
		do{
			sem_getvalue(usem->sem,&semval);
		}
		while(semval==1);
	}else{
		sem_wait(usem->sem);
		gettimeofday(&tv1, NULL);
	}
	for(i=0;i<MNUM;i++)
	{
		memset((void *)(mem+CHEAD_SIZE+capi.key_len),0,capi.iv_len);

		ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);

		while(!(mem[3]&DDONE_MASK));

	}
	sem_wait(dsem->sem);

	if(cpid==0)
	{
		free((void *)mem);
		exit(0);
	}else{
		do{
			sem_getvalue(dsem->sem,&semval);
		}
		while(semval!=(MAX_SEM_CNT-tcnt));
		gettimeofday(&tv2, NULL);
		free((void *)mem);
	}

	sem_post(usem->sem);
	for(i=0;i<tcnt;i++)
	{
		sem_post(dsem->sem);
	}

	time = (tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec;

	speed = MNUM*tcnt*capi.data_len/time;
	speed*=8;

	spd_dbg("%s,sha256 enc data_len %d,tcnt %d,speed is %dMbps,time %d us\n",__FILE__,capi.data_len,tcnt,speed,time);
}
hmac_sm3_ssl_hard_speed(int fd)
{
	int tcnt=100,MNUM=1000;
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,*dt,iv_tmp[16];
	unsigned int i;
	struct timeval tv1,tv2;

	int cpid;
	int semval;

	int time,speed;

	capi.alg = H_SM3;
	capi.type  = T_HMAC;
	capi.as = INITFINAL;
	capi.key_len = 32;
	capi.iv_len = 32;
	capi.data_len = (max_crypto_len-100)&0xfffffff0;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	for(i=0;i<tcnt-1;i++)
	{
		cpid = fork();
		if(cpid==0)
			break;
	}

	mem= malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

	if(cpid==0)
	{
		do{
			sem_getvalue(usem->sem,&semval);
		}
		while(semval==1);
	}else{
		sem_wait(usem->sem);
		gettimeofday(&tv1, NULL);
	}

	for(i=0;i<MNUM;i++)
	{
		memset((void *)(mem+CHEAD_SIZE+capi.key_len),0,capi.iv_len);

		ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);

		while(!(mem[3]&DDONE_MASK));
	}
	sem_wait(dsem->sem);

	if(cpid==0)
	{
		free((void *)mem);
		exit(0);
	}else{
		do{
			sem_getvalue(dsem->sem,&semval);
		}
		while(semval!=(MAX_SEM_CNT-tcnt));
		gettimeofday(&tv2, NULL);
		free((void *)mem);
	}

	sem_post(usem->sem);
	for(i=0;i<tcnt;i++)
	{
		sem_post(dsem->sem);
	}

	time = (tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec;

	speed = MNUM*tcnt*capi.data_len/time;
	speed*=8;

	spd_dbg("%s,sm3 enc data_len %d,tcnt %d,speed is %dMbps,time %d us\n",__FILE__,capi.data_len,tcnt,speed,time);

}
hash_sm3_ssl_hard_speed(int fd)
{
	int tcnt=100,MNUM=1000;
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,*dt,iv_tmp[16];
	unsigned int i;
	struct timeval tv1,tv2;

	int cpid;
	int semval;

	int time,speed;

	capi.alg = H_SM3;
	capi.type  = T_HASH;
	capi.as = INITFINAL;
	capi.key_len = 32;
	capi.iv_len = 32;
	//capi.data_len = (max_crypto_len-100)&0xfffffff0;
	capi.data_len = 16*1024;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	for(i=0;i<tcnt-1;i++)
	{
		cpid = fork();
		if(cpid==0)
			break;
	}

	mem= malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

	if(cpid==0)
	{
		do{
			sem_getvalue(usem->sem,&semval);
		}
		while(semval==1);
	}else{
		sem_wait(usem->sem);
		gettimeofday(&tv1, NULL);
	}

	for(i=0;i<MNUM;i++)
	{
		memset((void *)(mem+CHEAD_SIZE+capi.key_len),0,capi.iv_len);

		ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_HASH,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);

		while(!(mem[3]&DDONE_MASK));
	}
	sem_wait(dsem->sem);

	if(cpid==0)
	{
		free((void *)mem);
		exit(0);
	}else{
		do{
			sem_getvalue(dsem->sem,&semval);
		}
		while(semval!=(MAX_SEM_CNT-tcnt));
		gettimeofday(&tv2, NULL);
		free((void *)mem);
	}

	sem_post(usem->sem);
	for(i=0;i<tcnt;i++)
	{
		sem_post(dsem->sem);
	}

	time = (tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec;

	speed = MNUM*tcnt*capi.data_len/time;
	speed*=8;

	spd_dbg("%s,sm3 enc data_len %d,tcnt %d,speed is %dMbps,time %d us\n",__FILE__,capi.data_len,tcnt,speed,time);
}
